package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.powernode.constant.RedisConstant;
import com.powernode.dto.AppUserDto;
import com.powernode.dto.Result;
import com.powernode.entity.AppUser;
import com.powernode.entity.AppUserGroup;
import com.powernode.mapper.AppUserGroupMapper;
import com.powernode.mapper.AppUserMapper;
import com.powernode.service.AppUserGroupService;
import com.powernode.service.AppUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.util.RedisUtil;
import com.powernode.util.SmsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 * 网站用户 服务实现类
 * </p>
 *
 * @author Juice
 * @since 2023-02-08
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private AppUserGroupMapper groupMapper;
    /**
     * 根据电话获取验证码
     */
    @Override
    public Result<String> getLoginCode(String mobile) {
        // 获取验证码
        String verifyCode = smsUtil.sendVerification(mobile);
        // 将验证码存储到redis中，设置有效期为1分钟
        redisUtil.set(RedisConstant.KEY_AUTH_LOGIN_CODE+mobile,verifyCode,60);
        return Result.success("验证码发送成功",verifyCode);
    }

    /**
     * 校验验证码，并根据电话和验证码获取用户信息
     */
    @Override
    public Result<AppUserDto> getAppUserByMobileAndCode(String mobile, String code) {
        // 根据用户电话获取redis存储的验证码
        String redisCode = redisUtil.get(RedisConstant.KEY_AUTH_LOGIN_CODE + mobile);
        //验证码不一致返回false
        if (redisCode == null || !redisCode.equals(code)) {
            return Result.failure(HttpStatus.UNAUTHORIZED.value(), "验证码过期或无效");
        }
        // 根据电话获取用户信息
        LambdaQueryWrapper<AppUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppUser::getMobile,mobile);
        AppUser appUser = baseMapper.selectOne(wrapper);
        // 如果用户不存在返回false
        if (appUser == null) {
            return Result.failure(HttpStatus.UNAUTHORIZED.value(), "用户信息不存在");
        }

        // 获取用户组信息
        String groupName = Optional.ofNullable(groupMapper.selectById(appUser.getGroupId())).map(AppUserGroup::getGroupName).orElse(null);
        AppUserDto appUserDto = new AppUserDto();
        // 设置用户组信息
        appUserDto.setGroupName(groupName);
        BeanUtils.copyProperties(appUser,appUserDto);

        return Result.success("获取用户信息成功",appUserDto);
    }

    @Override
    public Result<AppUserDto> getAppUserByUsername(String username) {
        // 根据用户名获取用户对象
        AppUser appUser = baseMapper.selectOne(new QueryWrapper<AppUser>().eq("username", username));
        if (appUser == null) {
            return Result.failure(HttpStatus.NOT_FOUND.value(), "用户不存在");
        }
        // 获取用户组信息
        String groupName = Optional.ofNullable(groupMapper.selectById(appUser.getGroupId())).map(AppUserGroup::getGroupName).orElse(null);
        AppUserDto appUserDto = new AppUserDto();
        // 设置用户组信息
        appUserDto.setGroupName(groupName);
        BeanUtils.copyProperties(appUser,appUserDto);
        return Result.success("获取用户信息成功",appUserDto);
    }
}
